﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using MySql.Data.MySqlClient;

namespace YJKDataBase
{
	public class MySqlCategoryTable : ICategoryTable
	{
		public MySqlCategoryTable(RelationDatabase db)
		{
			this.mDatabase = db;
		}

		public bool Add(DbCategory category)
		{
			byte[] value = null;
			if (category.Image != null)
			{
				MemoryStream memoryStream = new MemoryStream();
				category.Image.Save(memoryStream, ImageFormat.Jpeg);
				value = memoryStream.ToArray();
			}
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			bool result;
			try
			{
				if (mySqlConnection.State == ConnectionState.Closed)
				{
					mySqlConnection.Open();
				}
				using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
				{
					int num;
					if (category.Limit)
					{
						num = 1;
					}
					else
					{
						num = 0;
					}
					mySqlCommand.CommandText = string.Format("insert into `CategoryTable` (`Name`,`ParentId`,`Limit`,`GUID`,`IMG`) values ('{0}','{1}','{2}','{3}',{4})", new object[]
					{
						category.Name,
						category.ParentId.ToString(),
						num,
						category.Guid.ToString(),
						"@img"
					});
					MySqlParameter mySqlParameter = new MySqlParameter("@img", DbType.Binary);
					mySqlParameter.Value = value;
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
					category.SetDatabase(this.mDatabase);
					if (mySqlConnection.State == ConnectionState.Open)
					{
						mySqlConnection.Close();
					}
					result = true;
				}
			}
			catch (Exception ex)
			{
				if (mySqlConnection.State == ConnectionState.Open)
				{
					mySqlConnection.Close();
				}
				throw new Exception("添加分类出错！\n" + ex.Message);
			}
			return result;
		}

		public bool Add(Parent parent, DbCategory category)
		{
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			bool result;
			try
			{
				if (mySqlConnection.State == ConnectionState.Closed)
				{
					mySqlConnection.Open();
				}
				using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
				{
					int num;
					if (category.Limit)
					{
						num = 1;
					}
					else
					{
						num = 0;
					}
					mySqlCommand.CommandText = string.Format("insert into `CategoryTable` (`Name`,`ParentId`,`Limit`,`GUID`,`IMG`) values ('{0}','{1}','{2}','{3}',{4})", new object[]
					{
						category.Name,
						parent.Guid.ToString(),
						num,
						category.Guid.ToString(),
						"?img"
					});
					MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
					if (category.Image != null)
					{
						MemoryStream memoryStream = new MemoryStream();
						category.Image.Save(memoryStream, ImageFormat.Jpeg);
						byte[] value = memoryStream.ToArray();
						mySqlParameter.Value = value;
					}
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
					category.SetParentId(parent.Guid);
					category.SetDatabase(this.mDatabase);
					if (mySqlConnection.State == ConnectionState.Open)
					{
						mySqlConnection.Close();
					}
					result = true;
				}
			}
			catch (Exception ex)
			{
				if (mySqlConnection.State == ConnectionState.Open)
				{
					mySqlConnection.Close();
				}
				throw new Exception("添加分类出错！\n" + ex.Message);
			}
			return result;
		}

		public bool Remove(DbCategory category)
		{
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			if (mySqlConnection.State == ConnectionState.Closed)
			{
				mySqlConnection.Open();
			}
			if (this.mDatabase.ExecuteCommand(string.Format("delete  from CategoryTable where GUID='{0}' and ParentId='{1}'", category.Guid.ToString(), category.ParentId)) > 0)
			{
				if (mySqlConnection.State == ConnectionState.Open)
				{
					mySqlConnection.Close();
				}
				return true;
			}
			if (mySqlConnection.State == ConnectionState.Open)
			{
				mySqlConnection.Close();
			}
			return false;
		}

		public bool Update(DbCategory category)
		{
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			bool result;
			try
			{
				int num;
				if (category.Limit)
				{
					num = 1;
				}
				else
				{
					num = 0;
				}
				if (mySqlConnection.State == ConnectionState.Closed)
				{
					mySqlConnection.Open();
				}
				using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
				{
					mySqlCommand.CommandText = string.Format("update CategoryTable set Name='{0}',ParentId='{1}',`Limit`='{2}',`Version`='{3}',Description='{4}',IMG={5} where GUID='{6}'", new object[]
					{
						category.Name,
						category.ParentId.ToString(),
						num,
						category.Version,
						category.Description,
						"?img",
						category.Guid.ToString()
					});
					MySqlParameter mySqlParameter = new MySqlParameter("?img", MySqlDbType.Blob);
					if (category.Image != null)
					{
						MemoryStream memoryStream = new MemoryStream();
						category.Image.Save(memoryStream, ImageFormat.Jpeg);
						byte[] value = memoryStream.ToArray();
						mySqlParameter.Value = value;
					}
					mySqlCommand.Parameters.Add(mySqlParameter);
					if (mySqlCommand.ExecuteNonQuery() != -1)
					{
						if (mySqlConnection.State == ConnectionState.Open)
						{
							mySqlConnection.Close();
						}
						result = true;
					}
					else
					{
						if (mySqlConnection.State == ConnectionState.Open)
						{
							mySqlConnection.Close();
						}
						result = false;
					}
				}
			}
			catch (Exception ex)
			{
				if (mySqlConnection.State == ConnectionState.Open)
				{
					mySqlConnection.Close();
				}
				throw new Exception("更新分类出错！\n" + ex.Message);
			}
			return result;
		}

		public DbCategory this[Guid id]
		{
			get
			{
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select `Limit`,`Name`,ParentId,GUID,IMG,Description,`Version` from CategoryTable where GUID='{0}'", id.ToString()));
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					DbCategory dbCategory = new DbCategory(dataTable.Rows[0][1].ToString().Trim(), new Guid(dataTable.Rows[0][2].ToString()));
					byte[] array = dataTable.Rows[0][4] as byte[];
					Image image;
					if (array != null)
					{
						image = new Bitmap(new MemoryStream(array));
					}
					else
					{
						image = new Bitmap(200, 200);
					}
					dbCategory.Image = image;
					dbCategory.SetId(new Guid(dataTable.Rows[0][3].ToString()));
					dbCategory.Limit = Convert.ToBoolean(dataTable.Rows[0][0]);
					dbCategory.Description = dataTable.Rows[0]["Description"].ToString();
					dbCategory.Version = dataTable.Rows[0]["Version"].ToString();
					dbCategory.SetDatabase(this.mDatabase);
					return dbCategory;
				}
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				return null;
			}
		}

		public DbCategory this[string name]
		{
			get
			{
				DbCategory dbCategory = null;
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select `Limit`,Name,ParentId,GUID,IMG,Description,`Version`  from CategoryTable where Name='{0}'", name));
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					dbCategory = new DbCategory(dataTable.Rows[0][1].ToString().Trim(), new Guid(dataTable.Rows[0][2].ToString()));
					byte[] array = dataTable.Rows[0][4] as byte[];
					Image image;
					if (array != null)
					{
						image = new Bitmap(new MemoryStream(array));
					}
					else
					{
						image = new Bitmap(200, 200);
					}
					dbCategory.Image = image;
					dbCategory.SetId(new Guid(dataTable.Rows[0][3].ToString()));
					dbCategory.Limit = Convert.ToBoolean(dataTable.Rows[0][0]);
					dbCategory.Description = dataTable.Rows[0]["Description"].ToString();
					dbCategory.Version = dataTable.Rows[0]["Version"].ToString();
					dbCategory.SetDatabase(this.mDatabase);
				}
				return dbCategory;
			}
		}

		public List<DbCategory> this[Parent parent]
		{
			get
			{
				List<DbCategory> list = new List<DbCategory>();
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select Name,`Limit`,ParentId,GUID,IMG,Description,`Version`  from CategoryTable where ParentId='{0}'", parent.Guid.ToString()));
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						DbCategory dbCategory = new DbCategory(dataTable.Rows[i][0].ToString().Trim(), new Guid(dataTable.Rows[i][2].ToString()));
						byte[] array = dataTable.Rows[i][4] as byte[];
						Image image;
						if (array != null)
						{
							image = new Bitmap(new MemoryStream(array));
						}
						else
						{
							image = new Bitmap(200, 200);
						}
						dbCategory.Image = image;
						dbCategory.SetId(new Guid(dataTable.Rows[i][3].ToString()));
						dbCategory.Limit = Convert.ToBoolean(dataTable.Rows[i][1]);
						dbCategory.Description = dataTable.Rows[i]["Description"].ToString();
						dbCategory.Version = dataTable.Rows[i]["Version"].ToString();
						dbCategory.SetDatabase(this.mDatabase);
						list.Add(dbCategory);
					}
				}
				return list;
			}
		}

		public List<DbCategory> GetAll()
		{
			List<DbCategory> list = new List<DbCategory>();
			if (this.mDatabase.dbCon.State == ConnectionState.Closed)
			{
				this.mDatabase.dbCon.Open();
			}
			DataTable dataTable = this.mDatabase.ExecQuery("select Name,`Limit`,ParentId,GUID,IMG,Description,Version  from CategoryTable");
			if (this.mDatabase.dbCon.State == ConnectionState.Open)
			{
				this.mDatabase.dbCon.Close();
			}
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					DbCategory dbCategory = new DbCategory(dataTable.Rows[i][0].ToString().Trim(), new Guid(dataTable.Rows[i][2].ToString()));
					byte[] array = dataTable.Rows[i][4] as byte[];
					Image image;
					if (array != null)
					{
						image = new Bitmap(new MemoryStream(array));
					}
					else
					{
						image = new Bitmap(200, 200);
					}
					dbCategory.Image = image;
					dbCategory.SetId(new Guid(dataTable.Rows[i][3].ToString()));
					dbCategory.Limit = Convert.ToBoolean(dataTable.Rows[i][1]);
					dbCategory.Description = dataTable.Rows[i]["Description"].ToString().Trim();
					dbCategory.Version = dataTable.Rows[i]["Version"].ToString();
					dbCategory.SetDatabase(this.mDatabase);
					list.Add(dbCategory);
				}
			}
			return list;
		}

		private RelationDatabase mDatabase;
	}
}
